package org.ThreadDemo.juctool;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        int threadCount = 5;
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        for (int i = 0; i < threadCount ; i++) {
            new Thread(() -> {
                try {
                    System.out.println(Thread.currentThread().getName() + " 执行开始"+System.currentTimeMillis() );
                    Thread.sleep(5000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " 执行完毕"+System.currentTimeMillis());
                countDownLatch.countDown();
            }).start();
        }

        countDownLatch.await();
        Thread.sleep(1000);
        System.out.println(Thread.currentThread().getName() + " 执行完毕"+System.currentTimeMillis());
        System.out.println("所有线程执行完毕");
    }
}